package com.ymttest.business.service;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import org.apache.http.client.ClientProtocolException;

import com.ymatou.iapi.coupon.parameter.old.BuyerInfo;
import com.ymatou.iapi.coupon.parameter.old.UserReceiveCouponBean;
import com.ymatou.iapi.coupon.parameter.old.UserReceivePackageBean;
import com.ymatou.iapi.couponmanage.old.parameter.CreateCouponBean;
import com.ymatou.iapi.couponmanage.old.parameter.CreateSendTaskBean;
import com.ymatou.iapi.couponmanage.old.parameter.SendCouponBean;
import com.ymatou.iapi.couponmanage.old.parameter.SendSmallCouponBean;
import com.ymatou.iapi.couponmanage.old.parameter.TaskSendReqInfo;
import com.ymatou.iapi.couponmanage.old.service.CreateCouponCall;
import com.ymatou.iapi.couponmanage.old.service.CreateSendTaskCall;
import com.ymatou.iapi.couponmanage.old.service.SendCouponCall;
import com.ymatou.iapi.m.couponservice.parameter.SellerCreateBatchBean;
import com.ymatou.promotion.api.parameter.CouponBatchBean;
import com.ymatou.promotion.api.service.CreateSellerCouponCall;
import com.ymt.core.tool.Logger;
import com.ymttest.utils.EnvSetup;

public class CouponManageCallService {
	
	public static CreateSendTaskBean CreateDefaultSendTaskBeanForPlatform(String batchCode) throws ClientProtocolException, IOException
	{
		Logger.comment("根据传入CouponCode创建平台券默认的CreateSendTaskBean");
		
		CreateSendTaskBean CreateSendTaskBean = new CreateSendTaskBean();
		TaskSendReqInfo taskSendReqInfo = new TaskSendReqInfo();
		
		taskSendReqInfo.setBatchCode(batchCode);
		
		int buyerId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
		HashSet<Integer> list = new HashSet();
		list.add(buyerId);
		
		//发放用户
		taskSendReqInfo.setSendUsers(list);
		//商家ID，0：代表平台
		taskSendReqInfo.setSellerId(0);
		// 操作者ID，对于商家可能是SellerId，对于平台是当前登录者ID
		taskSendReqInfo.setOperatorId(1111);
		// 操作者姓名，对于商家可能是商家姓名，对于平台是当前登录者姓名，不能为NULL
		taskSendReqInfo.setOperatorName("自动化OperatorName");
		//商家店铺名称，对于平台需要与产品确认，用于显示在通知消息内容里，不能为NULL
		taskSendReqInfo.setSellerShopName("自动化SellerShopName");
		// 业务类型，用于消息通知
		taskSendReqInfo.setBusinessType(1);
		CreateSendTaskBean.setTaskSendReqInfo(taskSendReqInfo);
		
		return CreateSendTaskBean;
	}
	public static CreateSendTaskBean CreateDefaultSendTaskBeanForPlatform() throws ClientProtocolException, IOException
	{
		Logger.comment("创建平台券默认的CreateSendTaskBean");
		CreateSendTaskBean CreateSendTaskBean = new CreateSendTaskBean();
		
		CouponCallService couponCallService = new CouponCallService();
		
		CreateCouponBean createCouponBean = couponCallService.setDefaultPlatformLinkCreateCouponBean();
		
		CreateCouponCall  createCouponCall = couponCallService.createPlatformCoupon(createCouponBean);
		
		TaskSendReqInfo taskSendReqInfo = new TaskSendReqInfo();
		
		taskSendReqInfo.setBatchCode(createCouponCall.getBatchCode());
		
		int buyerId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
		HashSet<Integer> list = new HashSet();
		list.add(buyerId);
		
		//发放用户
		taskSendReqInfo.setSendUsers(list);
		//商家ID，0：代表平台
		taskSendReqInfo.setSellerId(0);
		// 操作者ID，对于商家可能是SellerId，对于平台是当前登录者ID
		taskSendReqInfo.setOperatorId(1111);
		// 操作者姓名，对于商家可能是商家姓名，对于平台是当前登录者姓名，不能为NULL
		taskSendReqInfo.setOperatorName("自动化OperatorName");
		//商家店铺名称，对于平台需要与产品确认，用于显示在通知消息内容里，不能为NULL
		taskSendReqInfo.setSellerShopName("自动化SellerShopName");
		// 业务类型，用于消息通知
		taskSendReqInfo.setBusinessType(1);
		CreateSendTaskBean.setTaskSendReqInfo(taskSendReqInfo);
		
		return CreateSendTaskBean;
	}
	
	public static CreateSendTaskCall CreateSendTask(CreateSendTaskBean createSendTaskBean) throws ClientProtocolException, IOException
	{
		CreateSendTaskCall createsendtaskCall = new CreateSendTaskCall();

		createsendtaskCall.setData(createSendTaskBean);
		createsendtaskCall.callService();
		
		return createsendtaskCall;
	}
	
	public static SendCouponBean CreateDefaultSendCouponBean(String batchCode) throws ClientProtocolException, IOException
	{
		Logger.comment("根据传入BatchCode创建商家券默认的CreateSendTaskBean");
		SendCouponBean sendcouponBean = new SendCouponBean();

		TaskSendReqInfo taskSendReqInfo = new TaskSendReqInfo();
		
		taskSendReqInfo.setBatchCode(batchCode);
		
		int buyerId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
		HashSet<Integer> list = new HashSet();
		list.add(buyerId);
		
		//发放用户
		taskSendReqInfo.setSendUsers(list);
		//商家ID，0：代表平台
		taskSendReqInfo.setSellerId(Integer.valueOf(EnvSetup.getData("jCouponSellerId")));
		// 操作者ID，对于商家可能是SellerId，对于平台是当前登录者ID
		taskSendReqInfo.setOperatorId(Integer.valueOf(EnvSetup.getData("jCouponSellerId")));
		// 操作者姓名，对于商家可能是商家姓名，对于平台是当前登录者姓名，不能为NULL
		taskSendReqInfo.setOperatorName("自动化OperatorName");
		//商家店铺名称，对于平台需要与产品确认，用于显示在通知消息内容里，不能为NULL
		taskSendReqInfo.setSellerShopName("自动化SellerShopName");
		// 业务类型，用于消息通知
		taskSendReqInfo.setBusinessType(1);
		sendcouponBean.setTaskSendReqInfo(taskSendReqInfo);
		
		return sendcouponBean;
	}
	
	public static SendCouponBean CreateDefaultSendCouponBean() throws ClientProtocolException, IOException
	{
		Logger.comment("创建商家券默认的CreateSendTaskBean");
		SendCouponBean sendcouponBean = new SendCouponBean();
		
		CouponCallService couponCallService = new CouponCallService();
		
		CouponBatchBean createCouponBean = couponCallService.setDefaultCouponBatchBean();
		CreateSellerCouponCall  createCouponCall = couponCallService.createSellerCoupon(createCouponBean);
		
		TaskSendReqInfo taskSendReqInfo = new TaskSendReqInfo();
		
		taskSendReqInfo.setBatchCode(createCouponCall.getBatchCode());
		
		int buyerId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
		HashSet<Integer> list = new HashSet();
		list.add(buyerId);
		
		//发放用户
		taskSendReqInfo.setSendUsers(list);
		//商家ID，0：代表平台
		taskSendReqInfo.setSellerId(Integer.valueOf(EnvSetup.getData("jCouponSellerId")));
		// 操作者ID，对于商家可能是SellerId，对于平台是当前登录者ID
		taskSendReqInfo.setOperatorId(Integer.valueOf(EnvSetup.getData("jCouponSellerId")));
		// 操作者姓名，对于商家可能是商家姓名，对于平台是当前登录者姓名，不能为NULL
		taskSendReqInfo.setOperatorName("自动化OperatorName");
		//商家店铺名称，对于平台需要与产品确认，用于显示在通知消息内容里，不能为NULL
		taskSendReqInfo.setSellerShopName("自动化SellerShopName");
		// 业务类型，用于消息通知
		taskSendReqInfo.setBusinessType(1);
		sendcouponBean.setTaskSendReqInfo(taskSendReqInfo);
		
		return sendcouponBean;
	}
	
	public static SendCouponCall SendCoupon(SendCouponBean sendCouponBean) throws ClientProtocolException, IOException
	{
		Logger.comment("发送创建商家券请求");
			SendCouponCall sendcouponCall = new SendCouponCall();
			sendcouponCall.setData(sendCouponBean);
			sendcouponCall.callService();	
			return sendcouponCall;
	}
	
	public static SendSmallCouponBean setSendSmallCouponBean() throws ClientProtocolException, IOException
	{
		Logger.comment("创建商家券默认的SendSmallCouponBean");
		SendSmallCouponBean sendSmallCouponBean = new SendSmallCouponBean();
		
		HashSet<BuyerInfo> userInfos = new HashSet<>();
		
		BuyerInfo buyerInfo1 = new BuyerInfo();
		buyerInfo1.setBuyerId(11111);
		buyerInfo1.setBuyerOrderNum(0);
		
		BuyerInfo buyerInfo2 = new BuyerInfo();
		buyerInfo2.setBuyerId(22222);
		buyerInfo2.setBuyerOrderNum(0);
		
		BuyerInfo buyerInfo3 = new BuyerInfo();
		buyerInfo3.setBuyerId(33333);
		buyerInfo3.setBuyerOrderNum(0);
		
		userInfos.add(buyerInfo1);
		userInfos.add(buyerInfo2);
		userInfos.add(buyerInfo3);
		
		sendSmallCouponBean.setUserInfos(userInfos);
		
		// 创建优惠券批次
		SendCouponBean sendcouponBean = new SendCouponBean();
		
		CouponCallService couponCallService = new CouponCallService();
		
		CouponBatchBean createCouponBean = couponCallService.setDefaultCouponBatchBean();
		createCouponBean.setMaxSendNum(10000);
		CreateSellerCouponCall  createCouponCall = couponCallService.createSellerCoupon(createCouponBean);
		
		sendSmallCouponBean.setBatchCode(createCouponCall.getBatchCode());
		sendSmallCouponBean.setBusinessType(1);
		sendSmallCouponBean.setSellerId(Integer.valueOf(EnvSetup.getData("jCouponSellerId")));
		sendSmallCouponBean.setSellerShopName("Seller shop name");
		
		return sendSmallCouponBean;
	}
	
	
	public static SendSmallCouponBean setSendSmallCouponBean(String batchCode) throws ClientProtocolException, IOException
	{
		Logger.comment("根据传入batchCode创建商家券默认的SendSmallCouponBean");
		SendSmallCouponBean sendSmallCouponBean = new SendSmallCouponBean();
		
		HashSet<BuyerInfo> userInfos = new HashSet<>();
		
		BuyerInfo buyerInfo1 = new BuyerInfo();
		buyerInfo1.setBuyerId(11111);
		buyerInfo1.setBuyerOrderNum(0);
		
		BuyerInfo buyerInfo2 = new BuyerInfo();
		buyerInfo2.setBuyerId(22222);
		buyerInfo2.setBuyerOrderNum(0);
		
		BuyerInfo buyerInfo3 = new BuyerInfo();
		buyerInfo3.setBuyerId(33333);
		buyerInfo3.setBuyerOrderNum(0);
		
		userInfos.add(buyerInfo1);
		userInfos.add(buyerInfo2);
		userInfos.add(buyerInfo3);
		
		sendSmallCouponBean.setUserInfos(userInfos);
		
		sendSmallCouponBean.setBatchCode(batchCode);
		sendSmallCouponBean.setBusinessType(1);
		sendSmallCouponBean.setSellerId(Integer.valueOf(EnvSetup.getData("jCouponSellerId")));
		sendSmallCouponBean.setSellerShopName("Seller shop name");
		
		return sendSmallCouponBean;
	}
	
	public static UserReceiveCouponBean setUserReceiveCouponBean(String batchCode)
	{
		UserReceiveCouponBean userreceivecouponBean = new UserReceiveCouponBean();
		
		userreceivecouponBean .setUserId(Integer.valueOf(EnvSetup.getData("BuyUserId")));
		userreceivecouponBean.setBatchCode(batchCode);
		userreceivecouponBean.setBuyOrderNum(10);
		userreceivecouponBean.setBusinessType(1);
		userreceivecouponBean.setSellerShopName("商家店铺名称");
		
		return userreceivecouponBean;
	}
	
	public static UserReceivePackageBean setUserReceivePackageBean(List<String> batchCodes)
	{
		UserReceivePackageBean userReceivePackageBean = new UserReceivePackageBean();
		
		userReceivePackageBean .setUserId(Integer.valueOf(EnvSetup.getData("BuyUserId")));
		userReceivePackageBean.setBatchCodes(batchCodes);
		userReceivePackageBean.setBuyOrderNum(10);
		userReceivePackageBean.setBusinessType(1);
		userReceivePackageBean.setSellerShopName("商家店铺名称");
		
		return userReceivePackageBean;
	}
	
	public static void main(String args[]) {
        try {

			CouponServiceMCallService couponCallService = new CouponServiceMCallService();	

			// 商家优惠券
			SellerCreateBatchBean couponBatchBean = couponCallService.setDefaultSellerCreateBatchBean();
			couponBatchBean.setSellerId(21158981);
			couponBatchBean.setOrderMinAmount(new BigDecimal(1000));
			couponBatchBean.setCouponTotalCount(20000000);
			couponBatchBean.setReceiveNumPerUser(1);
			
			for(int i=0;i<20;i++)
			{
				String couponbatchString=couponCallService.SellerCreateBatch(couponBatchBean).getBatchCode();
			
            	System.out.println(couponbatchString);
			}
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
